package com.stoken.web.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stoken.domain.MemberExtractList;
import com.stoken.domain.TypeLineInfo;
import com.stoken.service.MemberExtractListService;
import com.stoken.web.control.vo.Message;
import com.symbio.commons.Compositor;
import com.symbio.commons.Filtration;
import com.symbio.commons.Filtration.MatchType;
import com.symbio.commons.Page;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.utils.WebUtils;

/**
 * 提现处理控制层
 * @author Fred
 *
 */
@Controller
@RequestMapping("/withdrawals")
public class MemberExtractControl {
	
	@Autowired
	MemberExtractListService memberExtractListService;
	
	/**
	 * 加载提现列表
	 * @return
	 */
	@RequestMapping("/backstage/list")
	public String loadWithDrawalsList(){
		
		return "/backstage/financial/withdrawalsList";		
	}
	
	@RequestMapping("/backstage/processWithrawals")
	public String loadProcessWithrawalsPage(ModelMap modeMape,Long withdrawalsId){
		MemberExtractList memberExtractList = memberExtractListService.get(withdrawalsId);
		modeMape.addAttribute("memberExtractList", memberExtractList);
		return "/backstage/financial/processWithrawals";
	}
	
	/**
	 * 加载提现列表数据
	 * @return
	 */
	@RequestMapping("/backstage/data")
    public String loadWithDrawalsData(HttpServletRequest request,ModelMap modeMape,String extractState){
		Page<MemberExtractList> pageData = new Page<MemberExtractList>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("id",CompositorType.DESC);
		pageData.setCompositor(compositor);
		List<Filtration> filtrations = new ArrayList<Filtration>();
		if(StringUtils.isEmpty(extractState)){
			extractState = "0";
		}
		Filtration filtrState = new Filtration(MatchType.EQ,Integer.valueOf(extractState),"extractState");
		filtrations.add(filtrState);
		pageData.setFiltrations(filtrations);
		
		pageData = memberExtractListService.find(pageData);		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
			
		return "/backstage/financial/withdrawalsData";		
    }
	
	@RequestMapping("/backstage/processExtract")
	public void processExtract(Long id,String extractState,HttpServletResponse response){
		if(id == null || StringUtils.isEmpty(extractState)){
			print(response,new Message(1,"操作失败"));
			return;
		}
		Message message = memberExtractListService.processExtract(id,Integer.parseInt(extractState));
		print(response,message);
		
	}
	
	/**
	 * 输出
	 * @param response
	 * @param message
	 */
	private void print(HttpServletResponse response,Message message){
		try {
			String json = "{\"code\":\""+message.getCode()+"\",\"context\":\""+message.getContext()+"\"}";  
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(json);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
	
}
